﻿using System.IO;
using System.Collections;
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class FileScrollViewComponent : MonoBehaviour 
{
    [SerializeField]
    Transform prefabsItemButtonSave;
    
    [SerializeField]
    Transform nodeGrid;

    [SerializeField]
    UIGrid scrollView;


    public enum FileOperationType
    {
        Read,
        Write,
        ReadPrefab,
    };
    public FileOperationType isRead;


    List<GameObject> saveItems = new List<GameObject>();

    public long GetTime(string path)
    {
        FileInfo fi = new FileInfo(path);
        return fi.CreationTime.Ticks;
    }
    public void ShowItem()
    {
        var paths = Directory.GetDirectories(Application.dataPath + "/" 
            + VoxelCategories.USERDATA_PATH + "/" + VoxelCategories.SAVE_PATH);

        foreach (GameObject item in saveItems)
        {
            Transform.Destroy(item);
        }
        saveItems.Clear();

        //排序
        var queryPaths = from path in paths
                         orderby GetTime(path) descending /*ascending*/
                         select path;

        foreach (string path in queryPaths)
        {
            string fileName =  Path.GetFileName(path);
            if (File.Exists(path + "/" + fileName + ".riovox"))//存在rvox
            {
                Transform tranItem = Instantiate(prefabsItemButtonSave);
                saveItems.Add(tranItem.gameObject);
                Texture2D img = new Texture2D(0, 0);
                if (File.Exists(path + "/" + fileName + ".png"))
                {
                    img.LoadImage(File.ReadAllBytes(path + "/" + fileName + ".png"));
                }

                tranItem.GetComponent<FileSaveItemComponent>().Show(
                    img, fileName ,this
                    );
                tranItem.SetParent(nodeGrid);
                //tranItem.parent = nodeGrid;
                tranItem.localScale = new Vector3(1, 1, 1);
            }
        }
        scrollView.enabled = true;
        scrollView.repositionNow = true;
        scrollView.Reposition();

        StartCoroutine(ScrollViewReposition());
    }

    void Start ()
    {
        ShowItem();
	}
	
	void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            scrollView.Reposition();
        }
    }

    public void Set_IsWrite()
    {
        isRead = FileOperationType.Write;
    }
    public void Set_IsRead()
    {
        isRead = FileOperationType.Read;
    }
    public void Set_IsReadPrefab()
    {
        isRead = FileOperationType.ReadPrefab;
    }

    public void NewSave()
    {
        UIComponentMastermind.instance.ShowInput(
            "请输入名称",//
            (fileName) =>
            {
                if (SaveReadWrite.IsFileExists(fileName))
                {
                    UIComponentMastermind.instance.ShowYesOrNo(
                      "是否覆盖存档 " + fileName,//
                      () =>
                      {
                          VoxelEditorMastermind.instance.WriteSave(fileName);
                          ShowItem();
                      },
                      () =>
                      {
                      }
                  );
                }
                else
                {
                    VoxelEditorMastermind.instance.WriteSave(fileName);
                    ShowItem();
                }

            },

            () =>
            {
                VoxelEditorMastermind.instance.ClearAll();
            }
        );
    }

    private IEnumerator ScrollViewReposition()
    {
        yield return new WaitForEndOfFrame();
        scrollView.enabled = true;
        scrollView.repositionNow = true;
        scrollView.Reposition();
    }

}
